home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
apps
/
sprdshts
/
viscalc.lzh
/
VISCALC
/
SC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-20
|
19KB
|
855 lines
/* SC A Spreadsheet Calculator
* Main driver
*
* original by James Gosling, September 1982
* modifications by Mark Weiser and Bruce Israel,
* University of Maryland
*
* More mods Robert Bond, 12/86
* Major mods to run on VMS and AMIGA, 1/17/87
*
*/
#include "sc.h"
extern char *malloc();
/* default column width */
#define DEFWIDTH 10
#define DEFPREC 2
#define RESCOL 4 /* columns reserved for row numbers */
#define RESROW 3 /* rows reserved for prompt, error, and column numbers */
char curfile[1024];
int showme = 1; /* 1 to display the current cell in the top line */
int batch = 0;
error (fmt,a,b,c,d,e) {
if (batch) fprintf(stderr,fmt,a,b,c,d,e);
else {
move (1,0);
clrtoeol ();
printw (fmt,a,b,c,d,e);
}
}
int seenerr;
yyerror (err)
char *err; {
if (seenerr) return;
seenerr++;
move (1,0);
clrtoeol ();
#ifdef TOS
{
char format[20];
sprintf(format,"%%s: %%.%ds <= %%s",linelim);
printw(format,err,line,line+linelim);
}
#else
printw ("%s: %.*s <= %s",err,linelim,line,line+linelim);
#endif
}
struct ent *
lookat(row,col){
register struct ent **p;
if (row < 0)
row = 0;
else if (row > MAXROWS-1)
row = MAXROWS-1;
if (col < 0)
col = 0;
else if (col > MAXCOLS-1)
col = MAXCOLS-1;
p = &tbl[row][col];
if (*p==0) {
*p = (struct ent *) malloc (sizeof (struct ent));
if (row>maxrow) maxrow = row;
if (col>maxcol) maxcol = col;
(*p)->label = 0;
(*p)->flags = 0;
(*p)->row = row;
(*p)->col = col;
(*p)->expr = 0;
(*p)->v = (double) 0.0;
} else if ((*p)->flags & is_deleted)
debug("But %s%d has been deleted!", coltoa(col), row);
return *p;
}
/*
* This structure is used to keep ent structs around before they
* are deleted to allow the sync_refs routine a chance to fix the
* variable references.
* We also use it as a last-deleted buffer for the 'p' command.
*/
free_ent(p)
register struct ent *p;
{
p->next = to_fix;
to_fix = p;
p->flags |= is_deleted;
}
flush_saved()
{
register struct ent *p;
register struct ent *q;
if (!(p = to_fix))
return;
while (p) {
clearent(p);
q = p->next;
free(p);
p = q;
}
to_fix = 0;
}
update () {
register row,
col;
register struct ent **p;
static lastmx,
lastmy;
static char *under_cursor = " ";
int maxcol;
int maxrow;
int rows;
int cols;
register r;
while (hidden_row[currow]) /* You can't hide the last row or col */
currow++;
while (hidden_col[curcol])
curcol++;
if (curcol < stcol)
stcol = curcol, FullUpdate++;
if (currow < strow)
strow = currow, FullUpdate++;
while (1) {
register i;
for (i = stcol, cols = 0, col = RESCOL;
(col + fwidth[i]) < COLS-1 && i < MAXCOLS; i++) {
cols++;
if (hidden_col[i])
continue;
col += fwidth[i];
}
if (curcol >= stcol + cols)
stcol++, FullUpdate++;
else
break;
}
while (1) {
register i;
for (i = strow, rows = 0, row = RESROW;
row < ROWS && i < MAXROWS; i++) {
rows++;
if (hidden_row[i])
continue;
row++;
}
if (currow >= strow + rows)
strow++, FullUpdate++;
else
break;
}
maxcol = stcol + cols - 1;
maxrow = strow + rows - 1;
if (FullUpdate) {
register int i;
move (2, 0);
clrtobot ();
standout();
for (row=RESROW, i=strow; i <= maxrow; i++) {
if (hidden_row[i])
continue;
move(row,0);
#ifdef TOS
{
char format[10];
sprintf(format,"%%-%dd",RESCOL);
printw(format,i);
}
#else
printw("%-*d", RESCOL, i);
#endif
row++;
}
move (2,0);
#ifdef TOS
{
char format[10];
sprintf(format,"%%%ds",RESCOL);
printw(format," ");
}
#else
printw("%*s", RESCOL, " ");
#endif
for (col=RESCOL, i = stcol; i <= maxcol; i++) {
if (hidden_col[i])
continue;
move(2, col);
#ifdef TOS
{
char format[10];
sprintf(format,"%%%ds",fwidth[i]);
printw(format,coltoa(i));
}
#else
printw("%*s", fwidth[i], coltoa(i));
#endif
col += fwidth[i];
}
standend();
}
for (row = strow, r = RESROW; row <= maxrow; row++) {
register c = RESCOL;
if (hidden_row[row])
continue;
for (p = &tbl[row][col = stcol]; col <= maxcol; col++, p++) {
if (hidden_col[col])
continue;
if (*p && ((*p) -> flags & is_changed || FullUpdate)) {
char *s;
move (r, c);
(*p) -> flags &= ~is_changed;
if ((*p) -> flags & is_valid)
#ifdef TOS
{
char format[16];
sprintf(format,"%%%d.%df",fwidth[col],precision[col]);
printw(format,(*p)->v);
}
#else
printw ("%*.*f", fwidth[col], precision[col], (*p) -> v);
#endif
if (s = (*p) -> label) {
char field[1024];
strncpy(field,s,fwidth[col]);
field[fwidth[col]] = 0;
move (r,(*p) -> flags & is_leftflush
? c : c - strlen (field) + fwidth[col]);
addstr(field);
}
}
c += fwidth[col];
}
r++;
}
move(lastmy, lastmx);
if (inch() == '<')
addstr (under_cursor);
lastmy = RESROW;
for (row = strow; row < currow; row++)
if (!hidden_row[row])
lastmy += 1;
lastmx = RESCOL;
for (col = stcol; col <= curcol; col++)
if (!hidden_col[col])
lastmx += fwidth[col];
move(lastmy, lastmx);
*under_cursor = inch();
addstr ("<");
move (0, 0);
clrtoeol ();
if (linelim >= 0) {
addstr (">> ");
addstr (line);
} else {
if (showme) {
register struct ent *p;
p = tbl[currow][curcol];
if (p && ((p->flags & is_valid) || p->label)) {
if (p->expr || !p->label) {
linelim = 0;
editexp(currow, curcol);
} else {
sprintf(line, "%s", p->label);
}
addstr("[");
addstr (line);
addstr("]");
linelim = -1;
} else {
addstr("[]");
}
}
move (lastmy, lastmx);
}
FullUpdate = 0;
}
void
quit()
{
endwin ();
exit();
}
main (argc, argv)
char **argv; {
int inloop = 1;
register int c;
int edistate = -1;
int arg = 1;
int narg;
int nedistate = -1;
int running = 1;
{
register i;
for (i = 0; i < MAXCOLS; i++) {
fwidth[i] = DEFWIDTH;
precision[i] = DEFPREC;
}
}
linelim = -1;
curfile[0]=0;
running = 1;
if (argc > 1 && (! strcmp(argv[1],"-b"))) {
argc--, argv++;
batch = 1;
}
if (! batch) {
initscr ();
}
initkbd();
if (argc > 1) {
strcpy(curfile,argv[1]);
readfile (argv[1],0);
}
modflg = 0;
if (batch) exit(0);
error ("VC 2.1 Type '?' for help.");
FullUpdate++;
while (inloop) { running = 1;
while (running) {
nedistate = -1;
narg = 1;
if (edistate < 0 && linelim < 0 && (changed || FullUpdate))
EvalAll (), changed = 0;
update ();
refresh ();
move (1, 0);
clrtoeol ();
fflush (stdout);
seenerr = 0;
if (((c = nmgetch ()) < ' ') || ( c == 0177 ))
switch (c) {
case ctl (z):
quit();
break;
case ctl (r):
FullUpdate++;
touchwin();
clear();
break;
default:
error ("No such command (^%c)", c + 0100);
break;
case ctl (b):
while (--arg>=0) {
if (curcol)
curcol--;
else
error ("At column A");
while(hidden_col[curcol] && curcol)
curcol--;
}
break;
case ctl (c):
running = 0;
break;
case ctl (f):
while (--arg>=0) {
if (curcol < MAXCOLS - 1)